package com.example.tool.ui;

import com.example.tool.action.ActionUtil;
import com.example.tool.utils.SwingUtil;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBTextField;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.Nullable;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;

/**
 * <p>Title: ConfigDialogWrapper</p >
 * Description: 弹出框的样式
 * <p>CreateTime: 2022/6/29 14:32</p>
 * @author FT
 */
public class ConfigDialogWrapper extends DialogWrapper {
  private final Project project;
  private TextFieldWithBrowseButton textFieldWithBrowseButton;
  private final JBTextField pluginID = new JBTextField();
  private final JBTextField staticDirectory = new JBTextField();


  public TextFieldWithBrowseButton getTextFieldWithBrowseButton() {
    return textFieldWithBrowseButton;
  }

  public JBTextField getPluginID() {
    return pluginID;
  }


  public JBTextField getStaticDirectory() {
    return staticDirectory;
  }


  public ConfigDialogWrapper(Project project) {
    super(true);
    this.project = project;
    setTitle("Config Information");
    setSize(550, 260);
    setResizable(false);
    init();
  }


  @Override
  public @Nullable JComponent getPreferredFocusedComponent() {
    return textFieldWithBrowseButton;
  }

  @Override
  @SuppressWarnings("All")
  protected @Nullable JComponent createCenterPanel() {
    textFieldWithBrowseButton = new TextFieldWithBrowseButton();
    FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(false, true, false, false, false, false);
    textFieldWithBrowseButton.addBrowseFolderListener(new TextBrowseFolderListener(fileChooserDescriptor));
    String[] information = ActionUtil.getInformation(project);
    if (information != null) {
      textFieldWithBrowseButton.setText(information[0]);
      pluginID.setText(information[1]);
      staticDirectory.setText(information[2]);
    } else {
      staticDirectory.setText("WebContent");
    }
    JPanel jPanel = new JPanel();
    jPanel.removeAll();
    jPanel.setLayout(new GridLayoutManager(4, 2));
    SwingUtil.addChild(jPanel, new JLabel("Select project location: "), 0, 0);
    SwingUtil.addChild(jPanel, textFieldWithBrowseButton, 0, 1, true);
    SwingUtil.addChild(jPanel, new JLabel("Plugin ID: "), 1, 0);
    pluginID.setToolTipText("插件id");
    SwingUtil.addChild(jPanel, pluginID, 1, 1, true);
    SwingUtil.addChild(jPanel, new JLabel("Static directory: "), 2, 0);
    staticDirectory.setToolTipText("编译项目目录");
    SwingUtil.addChild(jPanel, staticDirectory, 2, 1, true);
    JPanel tips = new JPanel();
    tips.setLayout(new GridLayoutManager(1, 2, JBUI.insets(10), -1, -1));
    tips.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new JBColor(new Color(-10591639), new Color(-10591639))), "说明"));
    SwingUtil.addChild(tips, new JLabel(SwingUtil.promptTextAction()), 0);
    SwingUtil.addChild(jPanel, tips, 3);
    return jPanel;
  }

  @Override
  protected void doOKAction() {
    if (textFieldWithBrowseButton.getText().equals("")
        || pluginID.getText().equals("")
        || staticDirectory.getText().equals("")) {
      Messages.showMessageDialog(project, "Please fill in all Input box!", "Error", HToolIcon.ModuleIcon);
    } else if (!SwingUtil.IsEnglish(pluginID.getText())) {
      Messages.showMessageDialog(project, "PluginID Must be English letters!", "Error", HToolIcon.ModuleIcon);
    } else {
      super.doOKAction();
    }
  }
}
